package controllers

import (
	"github.com/astaxie/beego"
	"anfang/models"
	"HTanfang/htmodels"
	"strconv"
	"time"
	"strings"
	"github.com/astaxie/beego/validation"
	"fmt"
)

type DeviceMessagesController struct {
	beego.Controller
}

//带了参数导致bootstrap table无法正常使用
func (c *DeviceMessagesController) HTDeviceMessagesList_html() {
	str_DeviceType := c.Ctx.Input.URL()[strings.LastIndex(c.Ctx.Input.URL(), "/")+1:]
	DeviceType, _ := strconv.Atoi(str_DeviceType)
	c.Data["DeviceType"] = DeviceType
	c.TplName = "devicemessages_list.html"
	c.Render()
}

func (c *DeviceMessagesController) HTDeviceMessagesList() {
	DeviceType, _ := c.GetInt("DeviceType")
	limit, _ := c.GetInt("limit")
	offset, _ := c.GetInt64("offset")
	orderby := ""
	if order := c.GetString("order"); order != "asc" {
		orderby += "-"
	}
	orderfiled := c.GetString("sort")
	orderby += orderfiled
	if orderfiled == "" {
		orderby = "-Id"
	}
	search := c.GetString("search")
	fileds := []string{"Id", "DeviceMessagesTime", "DeviceId", "Data"}
	DeviceMessages := []models.DeviceMessages{}
	htmodels.HTDeviceMessagesGetList(&DeviceMessages, fileds, DeviceType, offset, limit, search, "Id", orderby)

	count := int64(0)
	if mycount, err := htmodels.GetCacheInt64(fmt.Sprintf(`search:%sDeviceType%d`, search, DeviceType)); mycount == 0 || err != nil {
		count, _ = htmodels.HTDeviceMessagesGetCount(search, "Id", DeviceType)
		htmodels.PutIntoCache(fmt.Sprintf(`search:%sDeviceType%d`, search, DeviceType), count, time.Minute*10)
	} else {
		count = mycount
	}

	c.Data["json"] = map[string]interface{}{"rows": &DeviceMessages, "total": count}
	c.ServeJSON()
}

func (c *DeviceMessagesController) HTDeviceMessagesDetail() {
	Id, _ := c.GetInt64("Id")
	if Id != 0 {
		c.Data["update"] = true
		SetDeviceMessagesDetailInfo(c, Id)
	}
	mytype, _ := c.GetInt("type")
	c.Data["Type"] = mytype
	c.TplName = "devicemessages_detail.html"
}

func SetDeviceMessagesDetailInfo(c *DeviceMessagesController, Id int64) {
	DeviceMessages := models.DeviceMessages{Id: int64(Id)}
	htmodels.HTDeviceMessagesGetDetail(&DeviceMessages)
	c.Data["DeviceMessages"] = &DeviceMessages
}

func (c *DeviceMessagesController) HTDeviceMessagesAdd() {
	DeviceMessages := models.DeviceMessages{}
	if err := c.ParseForm(&DeviceMessages); err != nil {
		Message(&c.Controller, "", "输入数据不对,即将返回！"+err.Error(), "", true)
	}
	if time_up, err := time.Parse("2006-01-02 15:04:05", c.GetString("devicemessagestime")); err != nil {
		DeviceMessages.DeviceMessagesTime = time.Now()
	} else {
		DeviceMessages.DeviceMessagesTime = time_up
	}

	valide := validation.Validation{}
	if b, err := valide.Valid(&DeviceMessages); !b || err != nil {
		if !b {
			Message(&c.Controller, "", "数据验证失败，即将返回！", "", true)
		}
		if err != nil {
			Message(&c.Controller, "", "数据验证错误，即将返回！"+err.Error(), "", true)
		}
	}
	if err := htmodels.HTDeviceMessagesAdd(&DeviceMessages); err != nil {
		Message(&c.Controller, "", "添加失败，即将返回！"+err.Error(), "", true)
	}
	Message(&c.Controller, `/devicemessages/detail`, "添加成功！", "", false)
}

func (c *DeviceMessagesController) HTDeviceMessagesUpdate() {
	Id, _ := c.GetInt64("Id");
	DeviceMessages := models.DeviceMessages{Id: int64(Id)}
	if err := htmodels.HTDeviceMessagesGetDetail(&DeviceMessages); err != nil {
		Message(&c.Controller, "", "严重错误，请联系管理员,即将返回！"+err.Error(), "", true)
	}
	if err := c.ParseForm(&DeviceMessages); err != nil {
		Message(&c.Controller, "", "输入数据不对,即将返回！"+err.Error(), "", true)
	}
	if time_up, err := time.Parse("2006-01-02 15:04:05", c.GetString("devicemessagestime")); err != nil {
		DeviceMessages.DeviceMessagesTime = time.Now()
	} else {
		DeviceMessages.DeviceMessagesTime = time_up
	}

	valide := validation.Validation{}
	if b, err := valide.Valid(&DeviceMessages); !b || err != nil {
		if !b {
			Message(&c.Controller, "", "数据验证失败，即将返回！", "", true)
		}
		if err != nil {
			Message(&c.Controller, "", "数据验证错误，即将返回！"+err.Error(), "", true)
		}
	}
	if err := htmodels.HTDeviceMessagesUpdate(&DeviceMessages); err != nil {
		Message(&c.Controller, "", "更新失败，即将返回！"+err.Error(), "", true)
	}
	Message(&c.Controller, `/devicemessages/list_html`+`/`+strconv.Itoa(DeviceMessages.DeviceType), "更新成功！", "", false)
}

func (c *DeviceMessagesController) HTDeviceMessagesDelete() {
	Ids := make([]int64, 0, 100)
	c.Ctx.Input.Bind(&Ids, "Id")
	count, err := htmodels.HTDeviceMessagesDelete(Ids)
	if err != nil {
		Json_Message(&c.Controller, 0, "删除失败"+err.Error())
	}
	Json_Message(&c.Controller, 1, "删除"+strconv.FormatInt(count, 10)+"行")
}

func (c *DeviceMessagesController) HTDeviceGetMessagesLatest() {
	id, _ := c.GetInt("Id")
	DeviceId, _ := c.GetInt("DeviceId")
	fileds := []string{"Id", "DeviceMessagesTime", "DeviceId", "Data"}
	DeviceMessages := []models.DeviceMessages{}
	htmodels.HTDeviceMessageGetListById(&DeviceMessages, fileds, DeviceId, id)
	c.Data["json"] = DeviceMessages
	c.ServeJSON()
}

func (c *DeviceMessagesController) HTDeviceGetMessagesRecent() {
	DeviceId, _ := c.GetInt("DeviceId")
	fileds := []string{"Id", "DeviceMessagesTime", "DeviceId", "Data"}
	DeviceMessages := []models.DeviceMessages{}
	htmodels.HTDeviceMessagesGetListByDeviceId(&DeviceMessages, DeviceId, fileds, 0, 200)

	Devices := models.Devices{Id: int(DeviceId)}
	htmodels.HTDevicesGetDetail(&Devices)
	c.Data["json"] = map[string]interface{}{"Device": Devices, "MessageList": DeviceMessages}
	c.ServeJSON()
}
